import 'package:center_beijing/common/styles.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:flutter_screenutil/flutter_screenutil.dart';

class CustomInput extends StatefulWidget {
  final String title;
  final String placeHold;
  final Function textChange;
  final Function obscureChange;
  final Function focusChange;
  final bool eyeAble;
  final bool eye;
  final bool clearAll;
  final TextInputType keyboardType;
  final bool require;
  final int maxLength;
  final int maxNumber;
  final String tag;
  TextEditingController textInput;
  final List<TextInputFormatter> inputformat;
  final bool readOnly;
  final Color backColor;
  final double textFont;
  final TextAlign textAlign;
  final double borderWidth;
  final String defaultValue;
  final Color titleColor;

  CustomInput(
      {this.clearAll = false,
      this.keyboardType = TextInputType.text,
        this.maxNumber = 0,
        this.focusChange,
        this.backColor = Styles.MainBackgroundColor,
        this.borderWidth,
      this.title,
        this.textAlign = TextAlign.right,
        this.textFont,
        this.readOnly=false,
        this.titleColor=Styles.DetailColor,
        this.tag,
      this.maxLength,
      this.placeHold,
      this.textChange,
      this.eyeAble = false,
      this.eye = false,
      this.obscureChange,
      this.textInput,
      this.inputformat,
        this.defaultValue,
      this.require = false});

  @override
  _CustomInputState createState() => _CustomInputState();
}

class _CustomInputState extends State<CustomInput> {

  FocusNode _focusNode = FocusNode();
  var myTextInput = TextEditingController();
  @override
  void initState() {
    _focusNode.addListener(() {
      if (!_focusNode.hasFocus) {
        // TextField has lost focus
        if(widget.maxNumber!=0&&num.parse(widget.textInput.text)>widget.maxNumber){
          widget.textInput.text = widget.maxNumber.toString();
        }
        if(widget.focusChange!=null){
          widget.focusChange();
        }
      }
    });
    super.initState();
    if(widget.defaultValue!=null){
      myTextInput.text = widget.defaultValue;
    }
  }
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        border: widget.borderWidth!=null?Border.all(color: Styles.LineColor,width: widget.borderWidth!=null?widget.borderWidth:0):null,
        // color: widget.backColor,
      ),
      child: Padding(
        padding: EdgeInsets.fromLTRB(Styles.spacing8, 0,
            Styles.spacing16, 0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            widget.title!=null?Container(
              child: Padding(
                padding: EdgeInsets.fromLTRB(0, 0, Styles.spacing8, 0),
                child: Row(
                  children: <Widget>[
                    widget.require
                        ? Text(
                            '*',
                            style: TextStyle(color: Colors.red),
                          )
                        : Container(),
                    Text(widget.title,
                        style: TextStyle(fontSize: widget.textFont==null?Styles.MiddleFont17:widget.textFont,color: widget.titleColor))
                  ],
                ),
              ),
            ):Container(),
            widget.tag!=null?
            Text(widget.tag+'-',style: TextStyle(color: Styles.LeftTitleColor,fontSize: Styles.MiddleFont17),):Container(),
            Expanded(
              child: TextField(
                style: TextStyle(
                    fontSize: widget.textFont==null?Styles.MiddleFont17:widget.textFont,color: widget.readOnly?Styles.TitleColor:Styles.DetailColor
                ),
                readOnly: widget.readOnly,
                focusNode: _focusNode,
                maxLength: widget.maxLength,
                textAlign: widget.textAlign,
                controller: widget.textInput??=myTextInput,
                obscureText: widget.eye,
                keyboardType: widget.keyboardType,
                inputFormatters: widget.inputformat,
                decoration: InputDecoration(
                    hintStyle: TextStyle(fontSize: widget.textFont==null?Styles.MiddleFont17:widget.textFont,color: Styles.TitleColor),
                    hintText: widget.placeHold,
                    border: InputBorder.none,
                    suffixIcon: widget.clearAll
                        ? Container(
                      width: 20.0.w,
                      height: 20.0.h,
                      child: new IconButton(
                        alignment: Alignment.center,
                        padding: const EdgeInsets.all(0.0),
                        iconSize: 18.0.w,
                        icon: Icon(Icons.cancel),
                        onPressed: () {
                          widget.textInput.text = "";
                        },
                      ),
                    )
                        : null),
                onChanged: (value) {
                  if(widget.textChange!=null){
                    widget.textChange(value);
                  }
                },

              ),
            ),
            widget.eyeAble
                ? GestureDetector(
                    onTap: () => widget.obscureChange(),
                    child: widget.eye
                        ? Icon(Icons.visibility_off)
                        : Icon(Icons.visibility),
                  )
                : Container()
          ],
        ),
      ),
    );
  }
}
